package ar.com.mooral.crohn.application;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Hashtable;

import android.util.Log;
import ar.com.mooral.pojo.AlarmPojo;
import ar.com.mooral.pojo.BasePojo;
import ar.com.mooral.pojo.FoodPojo;

public class CrohnContext implements CrohnConstants{

	protected static boolean activityMissedMedicationLoaded;
	protected static boolean activityPatientSymptomLoaded;
	protected static boolean activityDepositionsLoaded;
	
	protected static int lastClickedTab;
	
	protected static String username;
	protected static String password;
	
	protected static String periodo;
	
	private static String recordsSelectedChoise;
	private static String recordsMaxRange;
	private static String recordsMinRange;
	
	protected static String patientCode;
	protected static String medicalCode;
	
	protected static String depositionTypeOld;
	
	protected static int dateDay;
	protected static int dateMonth;
	protected static int dateYear;
	protected static boolean dateLoaded;

	protected static int timeHour;
	protected static int timeMinute;
	protected static boolean timeLoaded;
	
	
	protected static String name = "";
	
	protected static String digestion = "";
	
	protected static String note = "";
	protected static boolean noteLoaded;

	protected static String foodName = "";
	protected static int foodId;
	protected static boolean foodLoaded;

	protected static String symptomName = "";
	protected static boolean symptomLoaded;
	
	protected static int medicationDosesQuantity;
	protected static String medicationDosesUnits = "";
	protected static boolean medicationDosesLoaded;

	protected static int medicationFrequencyQuantity;
	protected static int medicationFrequencyEach;
	protected static String medicationFrequencyTime = "";
	protected static boolean medicationFrequencyLoaded;

	protected static BasePojo basePojo;
	protected static AlarmPojo alarmPojo;


	public static String getDepositionTypeOld() {
		return depositionTypeOld;
	}

	public static void setDepositionTypeOld(String depositionTypeOld) {
		CrohnContext.depositionTypeOld = depositionTypeOld;
	}

	public static String getPeriodo() {
		return periodo;
	}

	public static void setPeriodo(String periodo) {
		CrohnContext.periodo = periodo;
	}
	
	public static boolean isActivityDepositionsLoaded() {
		return activityDepositionsLoaded;
	}

	public static void setActivityDepositionsLoaded(
			boolean activityDepositionsLoaded) {
		CrohnContext.activityDepositionsLoaded = activityDepositionsLoaded;
	}
	
	
	public static boolean isActivityPatientSymptomLoaded() {
		return activityPatientSymptomLoaded;
	}

	public static void setActivityPatientSymptomLoaded(
			boolean activityPatientSymptomLoaded) {
		CrohnContext.activityPatientSymptomLoaded = activityPatientSymptomLoaded;
	}

	public static boolean isActivityMissedMedicationLoaded() {
		return activityMissedMedicationLoaded;
	}

	public static void setActivityMissedMedicationLoaded(
			boolean activityMissedMedicationLoaded) {
		CrohnContext.activityMissedMedicationLoaded = activityMissedMedicationLoaded;
	}

	public static BasePojo getPojo() {
		return basePojo;
	}

	public static AlarmPojo getAlarmPojo() {
		return alarmPojo;
	}

	public static void setPojo(BasePojo basePojo) {
		CrohnContext.basePojo = basePojo;
	}
	
	public static void setAlarmPojo(AlarmPojo alarmPojo) {
		CrohnContext.alarmPojo = alarmPojo;
	}
	
	
	public static boolean isAlarmPojoLoaded() {
		if (CrohnContext.alarmPojo == null) {
			return false;
		} else {
			return true;
		}
	}
	
	
	public static boolean isPojoLoaded() {
		if (CrohnContext.basePojo == null) {
			return false;
		} else {
			return true;
		}
	}

	public static int getMedicationDosesQuantity() {
		return medicationDosesQuantity;
	}

	public static void setMedicationDosesQuantity(int medicationDosesQuantity) {
		CrohnContext.medicationDosesQuantity = medicationDosesQuantity;
	}

	public static String getMedicationDosesUnits() {
		return medicationDosesUnits;
	}

	public static void setMedicationDosesUnits(String medicationDosesUnits) {
		CrohnContext.medicationDosesUnits = medicationDosesUnits;
	}

	public static boolean isMedicationDosesLoaded() {
		return medicationDosesLoaded;
	}

	public static void setMedicationDosesLoaded(boolean medicationDosesLoaded) {
		CrohnContext.medicationDosesLoaded = medicationDosesLoaded;
		if(!medicationDosesLoaded){
			medicationDosesQuantity = 0;
			medicationDosesUnits = "";
		}
	}

	public static int getMedicationFrequencyQuantity() {
		return medicationFrequencyQuantity;
	}

	public static void setMedicationFrequencyQuantity(
			int medicationFrequencyQuantity) {
		CrohnContext.medicationFrequencyQuantity = medicationFrequencyQuantity;
	}

	public static int getMedicationFrequencyEach() {
		return medicationFrequencyEach;
	}

	public static void setMedicationFrequencyEach(int medicationFrequencyEach) {
		CrohnContext.medicationFrequencyEach = medicationFrequencyEach;
	}

	public static String getMedicationFrequencyTime() {
		return medicationFrequencyTime;
	}

	public static void setMedicationFrequencyTime(String medicationFrequencyTime) {
		CrohnContext.medicationFrequencyTime = medicationFrequencyTime;
	}

	public static boolean isMedicationFrequencyLoaded() {
		return medicationFrequencyLoaded;
	}

	public static void setMedicationFrequencyLoaded(
			boolean medicationFrequencyLoaded) {
		CrohnContext.medicationFrequencyLoaded = medicationFrequencyLoaded;
		if(!medicationFrequencyLoaded){
			medicationFrequencyQuantity = 0;
			medicationFrequencyEach 	= 0;
			medicationFrequencyTime 	= "";
		}
	}

	public static String getName() {
		return name;
	}

	public static void setName(String name) {
		CrohnContext.name = name;
	}

	public static String getUsername() {
		return username;
	}

	public static void setUsername(String username) {
		CrohnContext.username = username;
	}
	
	public static String getPassword() {
		return password;
	}

	public static void setPassword(String password) {
		CrohnContext.password = password;
	}
	
	public static String getSymptomName() {
		return symptomName;
	}

	public static void setSymptomName(String symptomName) {
		CrohnContext.symptomName = symptomName;
	}

	public static boolean isSymptomLoaded() {
		return symptomLoaded;
	}

	public static void setSymptomLoaded(boolean symptomLoaded) {
		CrohnContext.symptomLoaded = symptomLoaded;
		if(!symptomLoaded){
			symptomName = "";
		}
	}

	public static String getFoodName() {
		return foodName;
	}

	public static void setFoodName(String foodName) {
		((FoodPojo)CrohnContext.getPojo()).setName(foodName);
	}

	public static int getFoodId() {
		return foodId;
	}

	public static void setFoodId(int foodId) {
		CrohnContext.foodId = foodId;
	}

	public static boolean isFoodLoaded() {
		return foodLoaded;
	}

	public static void setFoodLoaded(boolean foodLoaded) {
		CrohnContext.foodLoaded = foodLoaded;
		if(!foodLoaded){
			foodName = "";
			foodId   = 0;
		}
	}

	public static String getNote() {
		return note;
	}

	public static void setNote(String note) {
		CrohnContext.note = note;
	}

	public static boolean isNoteLoaded() {
		return noteLoaded;
	}

	public static void setNoteLoaded(boolean noteLoaded) {
		CrohnContext.noteLoaded = noteLoaded;
		if(!noteLoaded){
			note = "";
		}
		
	}

	public static boolean isDateLoaded() {
		return dateLoaded;
	}

	public static void setDateLoaded(boolean dateLoaded) {
		CrohnContext.dateLoaded = dateLoaded;
		if(!dateLoaded){
			dateDay 	= 0;
			dateMonth 	= 0;
			dateYear 	= 0;
		}
	}

	public static boolean isTimeLoaded() {
		return timeLoaded;
	}

	public static void setTimeLoaded(boolean timeLoaded) {
		CrohnContext.timeLoaded = timeLoaded;
		if(!timeLoaded){
			timeHour	= 0;
			timeMinute	= 0;
		}
	}

	public static String getDateTime(){
		return getDateTime("dd/MM/yyyy");
	}

	public static String getDateTime(String format){
		
    	if (CrohnContext.isDateLoaded() && CrohnContext.isTimeLoaded()) {
    		return CrohnContext.getDate(format) + " " + CrohnContext.getTime();
    	} else {
    		return CrohnContext.getDateTimeNow(format);
    	}
		
	}
	
	public static String getTime(){

		Date date = new Date();
		date.setHours(getTimeHour());
		date.setMinutes(getTimeMinute());
		date.setSeconds(0);
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");

		return formatter.format(date);
	}
	
	public static String getHumanDate(String sDate) {

		String r = "";
		
		try {
			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
			Date date = (Date)formatter.parse(sDate);
   
			SimpleDateFormat mysqlFormatter = new SimpleDateFormat("EEEEE, MMMMM d h:mm a");
			r = mysqlFormatter.format(date);

       } catch (ParseException e) {
    	   	Log.v("CROHN", "ParseException: " + e.getMessage());
       }
	   
       return r;
	}    
	
	public static String getDate(){
		return getDate("dd/MM/yyyy");
	}
	public static String getDate(String format){
		
		if (format.equals("mysql")) {
			format = "yyyy-MM-dd";
		}
		
		Date date = new Date();
		date.setDate(getDateDay());
		date.setMonth(getDateMonth());
		date.setYear(getDateYear()-1900);

		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
	
	public static void setDate(String strDate){
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
		
		try{
			Date date = formatter.parse(strDate);
			
			setDateDay(date.getDate());
			setDateMonth(date.getMonth());
			setDateYear(date.getYear() + 1900);
			setDateLoaded(true);
			
			setTimeHour(date.getHours());
			setTimeMinute(date.getMinutes());
			setTimeLoaded(true);
		}catch(Exception e){
			setDateLoaded(false);
		}
		
	}

	public static String getDigestion() {
		return digestion;
	}

	public static void setDigestion(String digestion) {
		CrohnContext.digestion = digestion;
	}

	public static int getTimeHour() {
		return timeHour;
	}
	
	public static void setTimeHour(int timeHour) {
		CrohnContext.timeHour = timeHour;
	}
	
	public static int getTimeMinute() {
		return timeMinute;
	}
	
	public static void setTimeMinute(int timeMinute) {
		CrohnContext.timeMinute = timeMinute;
	}
	
	public static int getDateDay() {
		return dateDay;
	}
	
	public static void setDateDay(int dateDay) {
		CrohnContext.dateDay = dateDay;
	}
	
	public static int getDateMonth() {
		return dateMonth;
	}
	
	public static void setDateMonth(int dateMonth) {
		CrohnContext.dateMonth = dateMonth;
	}
	
	public static int getDateYear() {
		return dateYear;
	}
	
	public static void setDateYear(int dateYear) {
		CrohnContext.dateYear = dateYear;
	}
	
	public static String getPatientCode() {
		return patientCode;
	}
	
	public static void setPatientCode(String patientCode) {
		CrohnContext.patientCode = patientCode;
	}
	
	public static String getMedicalCode() {
		return medicalCode;
	}
	
	public static void setMedicalCode(String medicalCode) {
		CrohnContext.medicalCode = medicalCode;
	}
	
	public static String getNextId() {
		return Integer.toString((int)System.currentTimeMillis());
	}

	public static void resetTemporalValues() {
		setDateLoaded(false);
		setTimeLoaded(false);
		setNoteLoaded(false);
		setFoodLoaded(false);
		setSymptomLoaded(false);
		setMedicationDosesLoaded(false);
		setMedicationFrequencyLoaded(false);
		setPojo(null);
		setAlarmPojo(null);
		setActivityMissedMedicationLoaded(false);
		setActivityPatientSymptomLoaded(false);
		setActivityDepositionsLoaded(false);
		setRecordsMinRange(null);
		setRecordsMaxRange(null);
		setDepositionTypeOld(null);
	}

	public static String getDateTimeNow() {
		return CrohnContext.getDateTimeNow("dd/MM/yyyy");
	}
	
	public static String getDateTimeNow(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format + " HH:mm:ss");
		return formatter.format(new Date(System.currentTimeMillis()));
	}

	public static String getDateNow() {
        SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy" + " 00:00:00");
		return formatter.format(new Date(System.currentTimeMillis()));
	}

	public static Hashtable<String, String> painLevels = null;
	static{
		painLevels = new Hashtable<String, String>();
		painLevels.put(PAIN_LEVEL_ID_NO, PAIN_LEVEL_NAME_NO);
		painLevels.put(PAIN_LEVEL_ID_LIGHT, PAIN_LEVEL_NAME_LIGHT);
		painLevels.put(PAIN_LEVEL_ID_MODERATE, PAIN_LEVEL_NAME_MODERATE);
		painLevels.put(PAIN_LEVEL_ID_INTENSE, PAIN_LEVEL_NAME_INTENSE);
	}
	public static String getPainLevelName(String painLevelId){
		return (String)painLevels.get(painLevelId);
	}
	

	public static Hashtable<String, String> generalStatus = null;
	static{
		generalStatus = new Hashtable<String, String>();
		generalStatus.put(GENERAL_STATUS_ID_VERY_GOOD, GENERAL_STATUS_NAME_VERY_GOOD);
		generalStatus.put(GENERAL_STATUS_ID_REGULAR, GENERAL_STATUS_NAME_REGULAR);
		generalStatus.put(GENERAL_STATUS_ID_BAD, GENERAL_STATUS_NAME_BAD);
		generalStatus.put(GENERAL_STATUS_ID_VERY_BAD, GENERAL_STATUS_NAME_VERY_BAD);
		generalStatus.put(GENERAL_STATUS_ID_TOO_BAD, GENERAL_STATUS_NAME_TOO_BAD);
	}
	public static String getGeneralStatusName(String generalStatusId){
		return (String)generalStatus.get(generalStatusId);
	}
	
	public static Hashtable<String, String> lifeQualityIncidents = null;
	
	static{
		lifeQualityIncidents = new Hashtable<String, String>();
		lifeQualityIncidents.put(LIFE_QUALITY_INCIDENCE_ID_LOW, LIFE_QUALITY_INCIDENCE_NAME_LOW);
		lifeQualityIncidents.put(LIFE_QUALITY_INCIDENCE_ID_MIDDLE, LIFE_QUALITY_INCIDENCE_NAME_MIDDLE);
		lifeQualityIncidents.put(LIFE_QUALITY_INCIDENCE_ID_HIGH, LIFE_QUALITY_INCIDENCE_NAME_HIGH);
	}
	public static String getLifeQualityIncidenceName(String lifeQualityIncidenceId){
		return (String)lifeQualityIncidents.get(lifeQualityIncidenceId);
	}

	public static void setRecordsSelectedChoise(String choise) {
		CrohnContext.recordsSelectedChoise = choise;
	}
	public static String getRecordsSelectedChoise() {
		return CrohnContext.recordsSelectedChoise; 
	}
	public static void setRecordsMinRange(String minRange) {
		CrohnContext.recordsMinRange = minRange;
	}
	public static String getRecordsMinRange() {
		return CrohnContext.recordsMinRange;
	}
	public static void setRecordsMaxRange(String maxRange) {
		CrohnContext.recordsMaxRange = maxRange;
	}
	public static String getRecordsMaxRange() {
		return CrohnContext.recordsMaxRange;
	}

	public static void setLastClickedTab(int currentTab) {
		CrohnContext.lastClickedTab = currentTab;
	}
	
	public static int getLastClickedTab() {
		return CrohnContext.lastClickedTab;
	}

}
